﻿// See https://aka.ms/new-console-template for more information
using MQTTnet;
using MQTTnet.Client;
using MQTTnet.Client.Options;
using System;

Console.WriteLine("Hello, World!");


// 创建客户端配置
var options = new MqttClientOptionsBuilder()
    .WithTcpServer("112.33.111.160", 8084)
    .WithClientId("Client1") // 客户端ID
    .Build();

// 创建MQTT客户端实例
var mqttClient = new MqttFactory().CreateMqttClient();

// 处理连接成功事件
mqttClient.UseConnectedHandler(e =>
{
    Console.WriteLine("Connected to MQTT Broker");
});

// 处理连接断开事件
mqttClient.UseDisconnectedHandler(e =>
{
    Console.WriteLine("Disconnected from MQTT Broker");
});

// 处理接收到消息事件
mqttClient.UseApplicationMessageReceivedHandler(e =>
{
    Console.WriteLine($"Received message: {e.ApplicationMessage.Payload}");
});

// 连接到MQTT服务器
await mqttClient.ConnectAsync(options, CancellationToken.None);

Random r = new Random();

while (true)
{
    // 计量衡实时数据
    double grossWeight = double.Parse(DateTime.Now.ToString("1mm.ff"));
    var message1 = new MqttApplicationMessageBuilder()
        .WithTopic("CF/GWM/Blockbuster1/RtData")
        .WithPayload(grossWeight.ToString())
        .WithRetainFlag()
        .Build();
    await mqttClient.PublishAsync(message1, CancellationToken.None);

    // 重磅称重信息
    Dictionary<string, object> msg2Dictionary = new Dictionary<string, object>();
    msg2Dictionary.Add("spler", "兴仁市xxx");
    msg2Dictionary.Add("variety", "xx");
    msg2Dictionary.Add("carno", "贵Exxx41");
    msg2Dictionary.Add("originPlace", "-");
    msg2Dictionary.Add("destinationPlace", "-");
    msg2Dictionary.Add("cardno", "741B3EAA");
    msg2Dictionary.Add("grossWeight", grossWeight.ToString());
    msg2Dictionary.Add("grossWeightTime", DateTime.Now.ToString("yyyy.MM.dd-HH:mm"));
    msg2Dictionary.Add("tareWeight", "");
    msg2Dictionary.Add("netWeight", "");
    msg2Dictionary.Add("deduction", "-");
    msg2Dictionary.Add("tip", "-");
    var a = Newtonsoft.Json.JsonConvert.SerializeObject(msg2Dictionary);
    var message2 = new MqttApplicationMessageBuilder()
        .WithTopic("CF/GWM/Blockbuster1/WeightInfo")
        .WithPayload(Newtonsoft.Json.JsonConvert.SerializeObject(msg2Dictionary))
        .WithRetainFlag()
        .Build();
    await mqttClient.PublishAsync(message2, CancellationToken.None);

    //气动传输
    int CSPCSysHealth = int.Parse(DateTime.Now.ToString("mm"));
    
    var message3 = new MqttApplicationMessageBuilder()
        .WithTopic("CS/PC/SysHealth")
        .WithPayload(CSPCSysHealth.ToString())
        .WithRetainFlag()
        .Build();
    await mqttClient.PublishAsync(message3, CancellationToken.None);

    int CSPCSysSysCommissioningRate = int.Parse(DateTime.Now.ToString("ff"));
    var message4 = new MqttApplicationMessageBuilder()
        .WithTopic("CS/PC/SysCommissioningRate")
        .WithPayload(CSPCSysSysCommissioningRate.ToString())
        .WithRetainFlag()
        .Build();
    await mqttClient.PublishAsync(message4, CancellationToken.None);

    PneumaticConveying pneumaticConveying = new PneumaticConveying();
    PneumaticConveyingData pneumaticConveyingData = new PneumaticConveyingData();
    pneumaticConveyingData.routeID = r.Next(1,8);
    pneumaticConveyingData.startPoint = "A";
    pneumaticConveyingData.endPoint = "B";
    pneumaticConveyingData.action = "start";
    pneumaticConveying.data = new List<PneumaticConveyingData>() { pneumaticConveyingData };

    var message5 = new MqttApplicationMessageBuilder()
        .WithTopic("CS/PC/PneumaticConveying")
        .WithPayload(Newtonsoft.Json.JsonConvert.SerializeObject(pneumaticConveying))
        .WithRetainFlag()
        .Build();
    await mqttClient.PublishAsync(message5, CancellationToken.None);

    await Task.Delay(6000);
}

class PneumaticConveying
{
    public List<PneumaticConveyingData> data;
}

class PneumaticConveyingData
{
    public int routeID { get; set; }

    public string startPoint { get; set; }

    public string endPoint { get; set; }

    public string action { get; set; }
}
